﻿using Contract.Domain;
using Contract.Infrastructure.Repository;
using Contract.WebApi.Application.Commands;
using MediatR;

namespace Contract.WebApi.Application.CommandHandler
{
    public class SaveCoordinatesCommandHandler : IRequestHandler<SaveCoordinatesCommands, int>
    {
        private readonly IRepository<Domain.Contract> repository;

        public SaveCoordinatesCommandHandler(IRepository<Domain.Contract> repository)
        {
            this.repository = repository;
        }

        public async Task<int> Handle(SaveCoordinatesCommands request, CancellationToken cancellationToken)
        {
            var info = await repository.GetAsync(x => x.ContractNumber == request.Number);
            info.startLat = request.startLat;
            info.startLng = request.startLng;
            info.endLat = request.endLat;
            info.endLng = request.endLng;
            var ind = await repository.UpdateAsync(info);
            return ind;
        }
    }
}
